/*
 * @Author: yxiawen
 * @Date: 2021-11-24 10:29:43
 * @LastEditTime: 2022-01-24 15:56:13
 * @LastEditors: yxiawen
 * @Description:
 * @FilePath: /vite-project/src/store/modules/user.js
 */
const TOKEN = localStorage.getItem("token") || "";
const USERID = localStorage.getItem("userId") || "";
import router from "@/router";

export const state = {
  token: TOKEN,
  userId: USERID
};
export const actions = {
  setToken({ commit }, token) {
    commit("SET_TOKEN", token);
  },
  setUserId({ commit }, userId) {
    commit("SET_USERID", userId);
  },
  reLogin({ commit }, info) {
    commit("SET_TOKEN", "");
    commit("SET_USERID", "");
    router.push({
      name: "login"
    });
  }
};

export const mutations = {
  SET_TOKEN(state, token) {
    state.token = token;
    localStorage.setItem("token", token);
    router.push({ name: "BannerMgt" });
  },
  SET_USERID(state, userId) {
    state.userId = userId;
    localStorage.setItem("userId", userId);
  }
};

export const getters = {
  hasLogin: state => {
    if (state.token) {
      return true;
    } else {
      return false;
    }
  },
  getToken: state => state.token,
  getUserId: state => state.userId
};
